package com.lujianfei.powersearch.base

import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder

abstract class MovableAdapter<T, V:ViewHolder>: RecyclerView.Adapter<V>() {

    val datas = arrayListOf<T>()

    open fun onItemMove(fromPosition: Int, toPosition: Int):Boolean {
        //交换位置
        val items = datas
        val item = items[fromPosition]
        items.removeAt(fromPosition)
        items.add(toPosition, item)
        notifyItemMoved(fromPosition, toPosition)
        return true
    }

    fun getData():List<T> = datas

    override fun getItemCount() = datas.size

    fun setData(dataList:List<T>?) {
        datas.clear()
        dataList?.let {
            datas.addAll(it)
        }
        notifyDataSetChanged()
    }

    abstract fun onSelectedChanged(viewHolder: ViewHolder?)
    abstract fun clearView(viewHolder: ViewHolder?)
}